package com.ruoyi.api.controller;

import com.ruoyi.api.domain.UserEntity;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginParam;
import com.ruoyi.common.core.domain.model.RegistParam;
import com.ruoyi.common.google.GoogleAuthServie;
import com.ruoyi.framework.front.service.UserLoiginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Map;



@RestController
public class LoginController {

    @Autowired
    private GoogleAuthServie googleAuthServie;
    @Autowired
    private UserLoiginService userLoiginService;
    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/front/login")
    public AjaxResult login(@RequestBody LoginParam loginBody)
    {
        // 生成令牌
        Map<String,Object> map = userLoiginService.login(loginBody.getEmail(), loginBody.getPassWord());
        return AjaxResult.success(map);
    }

    /**
     * 注册
     *
     * @param registParam 注册信息
     * @return 结果
     */
    @PostMapping("/front/register")
    public AjaxResult regist(@RequestBody RegistParam registParam)
    {
        // 生成令牌
        Map<String,Object> map  = userLoiginService.regist(registParam);
        return AjaxResult.success(map);
    }

    /**
     * 注册
     *
     * @param registParam 注册信息
     * @return 结果
     */
    @PostMapping("/front/getCode")
    public AjaxResult getCode(@RequestBody RegistParam registParam)
    {
        // 生成code
        Boolean map  = userLoiginService.getCode(registParam);
        return AjaxResult.success(map);
    }

    /**
     * 注册
     *
     * @param registParam 注册信息
     * @return 结果
     */
    @PostMapping("/front/resetCodeConfirm")
    public AjaxResult resetCodeConfirm(@RequestBody RegistParam registParam)
    {
        // 生成code
        Boolean map  = userLoiginService.resetCodeConfirm(registParam);
        return AjaxResult.success(map);
    }

    /**
     * 注册
     *
     * @param registParam 注册信息
     * @return 结果
     */
    @PostMapping("/front/resetPassword")
    public AjaxResult resetPassword(@RequestBody RegistParam registParam)
    {
        // 生成code
        Boolean map  = userLoiginService.resetPassword(registParam);
        return AjaxResult.success(map);
    }

    //获得Google发起登录请求链接
//    @GetMapping("/googleLoginUrl")
//    public AjaxResult googleLoginUrl() throws IOException {
//        try {
//            return AjaxResult.success(googleAuthServie.authorizingUrl());
//        } catch (GeneralSecurityException e) {
//            throw new RuntimeException(e);
//        }
//
//    }
//
//
//    //从跳转到登录成功的页面 获取授权码 根据授权码获取登录用户信息
//    @GetMapping("/google-login")
//    public AjaxResult googleLogin(@RequestParam("code") String authorizationCode) throws IOException, GeneralSecurityException {
//        Map<String,String>  map =  googleAuthServie.authorizing(authorizationCode);
//        UserEntity entity = new UserEntity();
//        if(map.containsKey("email")){
//            entity.setEmail(map.get("email"));
//        }
//        if(map.containsKey("token")){
//            entity.setGoogleToken(map.get("token"));
//        }
//        if(map.containsKey("refreshToken")){
//            entity.setGoogleRefreshToken(map.get("refreshToken"));
//        }
//        Map<String,Object> res =  userLoiginService.loginByGoogle(entity);
//        return AjaxResult.success(res);
//    }

    @GetMapping("/google-callback")
    public AjaxResult googleCallback(@RequestParam("code") String authorizationCode) throws IOException {
        System.out.println("google-callback code = "+authorizationCode);

        return AjaxResult.success();
    }





}
